package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.modules.hc.entity.*;
import org.jeecg.modules.hc.mapper.*;
import org.jeecg.modules.hc.service.IHcCompanyDetailService;
import org.jeecg.modules.hc.service.IHcCompanyInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 企业信息
 * @Author: jeecg-boot
 * @Date:   2023-07-04
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class HcCompanyInfoServiceImpl extends ServiceImpl<HcCompanyInfoMapper, HcCompanyInfo> implements IHcCompanyInfoService {

    private HcCompanyInfoMapper companyInfoMapper;
    private HcCompanyMerchantInfoMapper companyMerchantInfoMapper;
    private HcCompanyPickupAddressMapper companyPickupAddressMapper;
    private HcCompanyDepartmentMapper companyDepartmentMapper;
    private HcCompanyBannerMapper companyBannerMapper;
    private IHcCompanyDetailService hcCompanyDetailService;

    @Override
    public List<HcCompanyInfo> getAllCompanyList() {
        return companyInfoMapper.getAllCompanyList();
    }

    @Override
    @Transactional
    public void saveNew(HcCompanyInfo hcCompanyInfo) {
        String companyId = UuidUtils.getUUID().split("-")[0].substring(0, 6);
        hcCompanyInfo.setId(companyId);
        companyInfoMapper.insert(hcCompanyInfo);
        //添加其他信息
        addOtherCompanyInfo(hcCompanyInfo);
    }

    @Override
    @Transactional
    public void updateNew(HcCompanyInfo hcCompanyInfo) {
        companyInfoMapper.updateById(hcCompanyInfo);
        //删除其他信息
        delOtherCompanyInfo(hcCompanyInfo.getId());
        //添加其他信息
        addOtherCompanyInfo(hcCompanyInfo);
    }

    @Override
    @Transactional
    public void removeNew(String hcCompanyInfoId) {
        HcCompanyInfo hcCompanyInfo = companyInfoMapper.selectOne(new LambdaQueryWrapper<HcCompanyInfo>().eq(HcCompanyInfo::getId, hcCompanyInfoId));
        //获取企业详情
        List<HcCompanyDetail> hcCompanyDetails = hcCompanyDetailService.list(new LambdaQueryWrapper<HcCompanyDetail>().eq(HcCompanyDetail::getCompanyId, hcCompanyInfoId));
        if (CollectionUtils.isNotEmpty(hcCompanyDetails)){
            for (HcCompanyDetail hcCompanyDetail : hcCompanyDetails) {
                String listingStoreIds = hcCompanyDetail.getListingStoreIds();
                List<String> merchantIds = Arrays.asList(listingStoreIds.split(","));
                for (String merchantId : merchantIds) {
                    //删除商户对应的商品信息和展示菜单
                    hcCompanyDetailService.deleteMerchant(merchantId,hcCompanyDetail.getId());
                }
            }
            hcCompanyDetailService.removeByIds(hcCompanyDetails.stream().map(HcCompanyDetail::getId).collect(Collectors.toList()));
        }
        //删除其他信息
        delOtherCompanyInfo(hcCompanyInfoId);

        companyInfoMapper.deleteById(hcCompanyInfo);
    }

    @Override
    public HcCompanyInfo getByCompId(String id) {
        return companyInfoMapper.getByCompId(id);
    }

    public void delOtherCompanyInfo(String hcCompanyInfoId){
        //删除取餐地址
        LambdaQueryWrapper<HcCompanyPickupAddress> pickupAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pickupAddressLambdaQueryWrapper.eq(HcCompanyPickupAddress::getCompanyId,hcCompanyInfoId);
        companyPickupAddressMapper.delete(pickupAddressLambdaQueryWrapper);

        LambdaQueryWrapper<HcCompanyDepartment> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(HcCompanyDepartment::getCompanyId,hcCompanyInfoId);
        companyDepartmentMapper.delete(departmentLambdaQueryWrapper);

        //删除企业banner信息
        LambdaQueryWrapper<HcCompanyBanner> companyBannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyBannerLambdaQueryWrapper.eq(HcCompanyBanner::getCompanyId,hcCompanyInfoId);
        companyBannerMapper.delete(companyBannerLambdaQueryWrapper);
    }


    //添加企业其他信息
    public void addOtherCompanyInfo(HcCompanyInfo hcCompanyInfo){
        //取餐地址
        List<HcCompanyPickupAddress> companyPickupAddresses = hcCompanyInfo.getCompanyPickupAddresses();
        if (CollectionUtils.isNotEmpty(companyPickupAddresses)){
            for (HcCompanyPickupAddress companyPickupAddress : companyPickupAddresses) {
                companyPickupAddress.setCompanyId(hcCompanyInfo.getId());
                companyPickupAddressMapper.insert(companyPickupAddress);
            }
        }
        //企业部门信息
        List<HcCompanyDepartment> companyDepartments = hcCompanyInfo.getCompanyDepartments();
        if (CollectionUtils.isNotEmpty(companyDepartments)){
            for (HcCompanyDepartment companyDepartment : companyDepartments) {
                companyDepartment.setCompanyId(hcCompanyInfo.getId());
                companyDepartmentMapper.insert(companyDepartment);
            }
        }
        //企业banner信息
        List<HcCompanyBanner> companyBanners = hcCompanyInfo.getCompanyBanners();
        if (CollectionUtils.isNotEmpty(companyBanners)){
            for (HcCompanyBanner companyBanner : companyBanners) {
                companyBanner.setCompanyId(hcCompanyInfo.getId());
                companyBannerMapper.insert(companyBanner);
            }
        }
    }
}
